package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.common.CommonResult;
import com.woniu.dto.ComplainDTO;
import com.woniu.service.ComplainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.naming.Name;
import java.util.List;

@Controller
@RequestMapping("/complain")
@Api(tags = "物业投诉模块")
public class ComplainController {

    @Autowired
    private ComplainService complainService;

    /**
     * 分页查询投诉订单列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("sys:pmc:list")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面数据条数",defaultValue = "5")
    })
    public CommonResult list(@RequestParam(value = "page",defaultValue = "1",required = false)Integer currentPage,
                             @RequestParam(value = "limit",defaultValue = "5",required = false)Integer pageSize){
        PageInfo info=null;
        try {
            List<ComplainDTO> list=complainService.findByPage(currentPage,pageSize);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    /**
     * 导航到回复投诉的页面
     * @param id
     * @return
     */
    @RequestMapping("/reply/{id}")
    @ResponseBody
    @ApiOperation(value = "导航到回复投诉的页面")
    @ApiImplicitParam(name = "id",value = "列表id",dataType = "Integer")
    public ModelAndView reply(@PathVariable("id")Integer id){
        System.out.println("传入的id值为："+id);
        ComplainDTO complainDTO=complainService.findById(id);
        ModelAndView model=new ModelAndView("sys/reply");
        model.addObject("complainDTO",complainDTO);
        return model;
    }

    /**
     * 投诉回复内容的添加
     * @param complainDTO
     * @return
     */
    @PutMapping("/editComplain")
    @ResponseBody
    @RequiresPermissions("sys:pmc:reply")
    @ApiOperation("投诉回复")
    @ApiImplicitParam(name = "complainDTO",value = "回复内容",required = true)
    public CommonResult addReply(ComplainDTO complainDTO){

        try{
            complainService.addReply(complainDTO);
            return CommonResult.success("回复上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("回复上传失败");
        }

    }

    /**
     * 页面的查询功能
     * @param userName
     * @param status
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    @ApiOperation("页面查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名称"),
            @ApiImplicitParam(name = "status",value = "审批状态")
    })
    public CommonResult select(String userName,Integer status){

        PageInfo info=null;
        try {
            List<ComplainDTO> list=complainService.select(userName,status);
            info=new PageInfo(list);
            return CommonResult.success(info);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
